﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataAccess.WorkingCapitalLoan;
using FOModels.WorkingCapitalLoan;
using System.Data;
using System.Net;
namespace Bussiness.WorkingCapitalLoan
{
    public class WorkingCapitalLoanBLL
    {
        private WorkingCapitalLoanDAL dal;
        public WorkingCapitalLoanBLL()
        {
            dal = new WorkingCapitalLoanDAL();
        }
        public List<WorkingCapitalLoanApplyModels> getcontract()
        {
            List<WorkingCapitalLoanApplyModels> list = new List<WorkingCapitalLoanApplyModels>();
            WorkingCapitalLoanApplyModels fur;
            DataTable tb = dal.getcontract();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanApplyModels();
                    fur.WorkingCapitalLoanApplyID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.WorkingCapitalLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<WorkingCapitalLoanApplyModels> getcontractbystatuscount(int statusid)
        {
            List<WorkingCapitalLoanApplyModels> list = new List<WorkingCapitalLoanApplyModels>();
            WorkingCapitalLoanApplyModels fur;
            DataTable tb = dal.getcontractbystatuscount(statusid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanApplyModels();
                    fur.WorkingCapitalLoanApplyID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.WorkingCapitalLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public WorkingCapitalLoanApplyModels getcontractbyid(int WorkingCapitalLoanapplyid)
        {

            WorkingCapitalLoanApplyModels fur = new WorkingCapitalLoanApplyModels();
            DataTable tb = dal.getcontractbyid(WorkingCapitalLoanapplyid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanApplyModels();
                    fur.WorkingCapitalLoanApplyID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanID = int.Parse(row[1].ToString());
                    fur.CustomerName = row[2].ToString();
                    fur.CustomerEmail = row[3].ToString();
                    fur.CustomerPhone = row[4].ToString();
                    fur.CustomerIncome = row[5].ToString();
                    fur.CustomerDOB = row[6].ToString();
                    fur.StatusID = int.Parse(row[7].ToString());
                    fur.AppDate = row[8].ToString();
                    fur.WorkingCapitalLoanName = row[10].ToString();
                    fur.StatusName = row[17].ToString();
                    fur.CompanyImg = row[19].ToString();
                    fur.CompanyName = row[26].ToString();

                }
            }
            return fur;
        }
        public int WorkingCapitalLoanApply(int WorkingCapitalLoanID, WorkingCapitalLoanApplyModels WorkingCapitalLoan)
        {
            if (dal.WorkingCapitalLoanApply(WorkingCapitalLoanID, WorkingCapitalLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteWorkingCapitalLoanApply(int WorkingCapitalLoanApplyID)
        {
            if (dal.DeleteWorkingCapitalLoanApply(WorkingCapitalLoanApplyID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int UpdateStatus(WorkingCapitalLoanApplyModels WorkingCapitalLoan)
        {
            if (dal.UpdateStatus(WorkingCapitalLoan) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public WorkingCapitalLoanModels getTOP1()
        {

            WorkingCapitalLoanModels fur = new WorkingCapitalLoanModels();
            DataTable tb = dal.getTOP1();
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                }
                return fur;
            }
            else
                return null;
        }
        public List<WorkingCapitalLoanModels> getall(int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getall(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {

                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = row[3].ToString();
                    fur.MaximumBalance = row[4].ToString();
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.FlagImage = row[27].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public WorkingCapitalLoanModels getWorkingCapitalLoanByID(int WorkingCapitalLoanID, int langid)
        {

            WorkingCapitalLoanModels fur = new WorkingCapitalLoanModels();
            DataTable tb = dal.getWorkingCapitalLoanByID(WorkingCapitalLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public WorkingCapitalLoanModels getWorkingCapitalLoanByIDManager(int WorkingCapitalLoanID, int langid)
        {

            WorkingCapitalLoanModels fur = new WorkingCapitalLoanModels();
            DataTable tb = dal.getWorkingCapitalLoanByIDManager(WorkingCapitalLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                }
                return fur;
            }
            else
                return null;
        }
        public List<WorkingCapitalLoanModels> getWorkingCapitalLoanDefault(int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getWorkingCapitalLoanDefault(langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<WorkingCapitalLoanModels> getWorkingCapitalLoanrelati(int CompanyID, int HomeLoanID, int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getWorkingCapitalLoanrelati(CompanyID, HomeLoanID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<WorkingCapitalLoanModels> getWorkingCapitalLoan(int planid, string balance, int years, int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getWorkingCapitalLoan(planid, balance, years, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<WorkingCapitalLoanModels> getWorkingCapitalLoanByProvider(int planid, string balance, int years, int CompanyID, int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getWorkingCapitalLoanByProvider(planid, balance, years, CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public List<WorkingCapitalLoanModels> getWorkingCapitalLoanDefaultByProvider(int CompanyID, int langid)
        {
            List<WorkingCapitalLoanModels> list = new List<WorkingCapitalLoanModels>();
            WorkingCapitalLoanModels fur;
            DataTable tb = dal.getWorkingCapitalLoanDefaultByProvider(CompanyID, langid);
            if (tb != null && tb.Rows.Count > 0)
            {
                foreach (DataRow row in tb.Rows)
                {
                    fur = new WorkingCapitalLoanModels();
                    fur.WorkingCapitalLoanID = int.Parse(row[0].ToString());
                    fur.WorkingCapitalLoanName = row[9].ToString();
                    fur.CompanyID = int.Parse(row[2].ToString());
                    fur.Benefit = WebUtility.HtmlDecode(row[10].ToString());
                    fur.MinimumBalance = String.Format("{0:0,0}", double.Parse(row[3].ToString())).Replace(',', '.');
                    fur.MaximumBalance = String.Format("{0:0,0}", double.Parse(row[4].ToString())).Replace(',', '.');
                    fur.HomeLoanPlanID = int.Parse(row[5].ToString());
                    fur.Description = WebUtility.HtmlDecode(row[11].ToString());
                    fur.FeeContent = WebUtility.HtmlDecode(row[12].ToString());
                    fur.MoreInfo = WebUtility.HtmlDecode(row[13].ToString());
                    fur.Display = bool.Parse(row[6].ToString());
                    fur.CompanyName = row[22].ToString();
                    fur.CompanyImg = row[15].ToString();
                    fur.InterestVND = float.Parse(row[26].ToString());
                    list.Add(fur);
                }
            }
            return list;
        }
        public int UpdateWorkingCapitalLoan(WorkingCapitalLoanModels models, int langid)
        {
            if (dal.UpdateWorkingCapitalLoan(models) > 0 && dal.UpdateWorkingCapitalLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertWorkingCapitalLoan(WorkingCapitalLoanModels models)
        {
            if (dal.InsertWorkingCapitalLoan(models) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int InsertWorkingCapitalLoanLang(WorkingCapitalLoanModels models, int langid)
        {
            if (dal.InsertWorkingCapitalLoanLang(models, langid) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
        public int DeleteWorkingCapitalLoan(int CarLoanID)
        {
            if (dal.DeleteWorkingCapitalLoanLang(CarLoanID) > 0 && dal.DeleteWorkingCapitalLoan(CarLoanID) > 0)
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }
    }
}
